package cz.apnetis.fitweb.server.entity;

import java.util.Map;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Index;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Queries;
import javax.jdo.annotations.Query;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;

import com.google.appengine.api.datastore.Key;

import cz.apnetis.fitweb.shared.Setting;
import cz.apnetis.fitweb.shared.dto.SettingDTO;

/** Settings which is send to client
 * @author Lukas "benzin" Benda <a href="mailto:luk.benda@gmail.com">luk.benda@gmail.com</a>
 */
@Queries ({
  @Query(name = "s.userId", language = "JDOQL",
      value = "select from cz.apnetis.fitweb.server.entity.ClientSettingEntity where userId == :userId"
  )
})
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Version(strategy = VersionStrategy.VERSION_NUMBER, column="version_number")
public class ClientSettingEntity {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key; public Key getKey() { return this.key; } public void setKey(final Key newKey) { this.key = newKey; }
  @Persistent
  @Index
  private String userId; public String getUserId() { return this.userId; } public void setUserId(final String userId) { this.userId = userId; }
  @Persistent(serialized = "true")
  private Map<String, String> clientSettings; public void setClientSettings(final Map<String, String> clientSettings) { this.clientSettings = clientSettings; } public Map<String, String> getClientSettings() { return this.clientSettings; }

  public final Setting toSetting() {
    final Setting result = new SettingDTO();
    result.setClientSettings(getClientSettings());
    return result;
  }
}
